package com.infore.dataAudit.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateFormatUtil {

    public DateFormatUtil() {
    }
    
    public static void main(String[] args) {
        getMonthDayList("2018-05-10");
    }
    
    /**
     * 获取当月的每一天
     * 
     * 本月直到前一天
     * @author 蔡永顺
     * @return List<String>
     * @param time
     * @return
     * @date 2019年5月10日下午5:26:35
     */
    public static List<String> getMonthDayList(String time){
        List<String> timeDayList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(time);
            Calendar cal = Calendar.getInstance();
            int monthToday = cal.get(Calendar.MONTH);
            int yearToday = cal.get(Calendar.YEAR);
            int maxDay = cal.get(Calendar.DAY_OF_MONTH);
            cal.setTime(date);
            cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DAY_OF_MONTH));
            Date startDate = cal.getTime();
            int month = cal.get(Calendar.MONTH);
            int year = cal.get(Calendar.YEAR);
            Date endDate = null;
            if(month == monthToday && year == yearToday) {
                //传入的是当月
                cal.setTime(new Date());
                cal.set(Calendar.DAY_OF_MONTH, maxDay - 1);
                endDate = cal.getTime();
            }else {
                 maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                 cal.set(Calendar.DAY_OF_MONTH, maxDay);
                 endDate = cal.getTime();
            }
            cal.setTime(startDate);
            //System.out.println("monTh day s: "+sdf.format(cal.getTime()));
            //System.out.println("monTh day e: "+sdf.format(endDate));
            while(cal.getTimeInMillis() <= endDate.getTime()) {
                System.out.println("monTh day: "+sdf.format(cal.getTime()));
                timeDayList.add(sdf.format(cal.getTime()));
                cal.add(Calendar.DAY_OF_MONTH, 1);
            }
            
            
            
            System.out.println("当月最后一天： "+maxDay);
            
        } catch (ParseException e) {
            return timeDayList; 
        }
        return timeDayList; 
    }
    

    /**
     * 
     * @Description: 格式化日期
     * @param date
     * @param pattern
     * @return
     * @date 2018年3月16日
     * @author cys
     */
    public static String formatDtae(Date date, String pattern) {
        if (StringUtils.isBlank(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    // public static final SimpleDateFormat ISO8601 = new
    // SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss+08:00");
    /**
     * ISO8601标准格式
     * 
     * @author 蒋恒涛
     * @return DateFormat
     * @date 2018年3月2日上午10:50:49
     */
    public static DateFormat getISO8601() {
        return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss+08:00");
    }

    /**
     * 获取给定cal所在季度的第一天0:0:00及最后一天23:59:59，方法调用后三个参数引用都被修改，cal对应end的日期
     * 
     * @author 蒋恒涛
     * @return void
     * @param start
     * @param end
     * @param cal
     *            不能为null
     * @date 2018年3月2日上午10:51:10
     */
    public static void getFirstAndLastDayOfQuarter(Date start, Date end, Calendar cal) {
        int month = cal.get(Calendar.MONTH);
        int quarter = month / 3;

        cal.set(Calendar.MONTH, quarter * 3);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        if (start != null) {
            start.setTime(cal.getTime().getTime());
        }
        cal.add(Calendar.MONTH, 3); // 日期加一周期
        cal.add(Calendar.SECOND, -1);
        if (end != null) {
            end.setTime(cal.getTime().getTime());
        }
    }

    /**
     * 获取给定cal所在月份的第一天0:0:00及最后一天23:59:59，方法调用后三个参数引用都被修改，cal对应end的日期
     * 
     * @author 蒋恒涛
     * @return void
     * @param start
     * @param end
     * @param cal 不能为null
     * @date 2018年3月2日上午10:53:03
     */
    public static void getFirstAndLastDayOfMonth(Date start, Date end, Calendar cal) {
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        if (start != null) {
            start.setTime(cal.getTime().getTime());
        }
        cal.add(Calendar.MONTH, 1); // 日期加一周期
        cal.add(Calendar.SECOND, -1);
        if (end != null) {
            end.setTime(cal.getTime().getTime());
        }

    }

    /**
     * 获取给定cal所在季度的第一天0:0:00及最后一天23:59:59，方法调用后三个参数引用都被修改，cal对应end的日期
     * 
     * @author 蒋恒涛
     * @return void
     * @param start
     * @param end
     * @param cal
     * @date 2018年3月2日上午10:53:28
     */
    public static void getFirstAndLastDayOfWeek(Date start, Date end, Calendar cal) {
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        if (start != null) {
            start.setTime(cal.getTime().getTime());
        }
        cal.add(Calendar.DAY_OF_MONTH, 7); // 日期加一周期
        cal.add(Calendar.SECOND, -1);
        if (end != null) {
            end.setTime(cal.getTime().getTime());
        }
    }

    public static void getFirstAndLastTimeOfDay(Date start, Date end, Calendar cal) {
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        if (start != null) {
            start.setTime(cal.getTime().getTime());
        }
        cal.add(Calendar.DAY_OF_MONTH, 1); // 日期加一周期
        cal.add(Calendar.SECOND, -1);
        if (end != null) {
            end.setTime(cal.getTime().getTime());
        }
    }
    /**
     * 按格式识别日期
     * @author 蒋恒涛
     * @return Date
     * @param dateString
     * @param pattern
     * @date 2018年5月31日下午4:50:47
     */
    public static Date parseDate(String dateString, String pattern) {
        if(pattern == null || pattern.trim().isEmpty()) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    /**
     * 获取给定cal所在年的第一天0:0:00及最后一天23:59:59，方法调用后三个参数引用都被修改，cal对应end的日期
     * 
     * @author 蒋恒涛
     * @return void
     * @param start
     * @param end
     * @param cal
     * @date 2018年3月2日上午10:53:28
     */
    public static void getFirstAndLastDayOfYear(Date start, Date end, Calendar cal) {
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        if (start != null) {
            start.setTime(cal.getTime().getTime());
        }
        cal.add(Calendar.YEAR, 1); // 日期加一周期
        cal.add(Calendar.SECOND, -1);
        if (end != null) {
            end.setTime(cal.getTime().getTime());
        }
        
    }
}
